home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 3 code / ISO 9660 & High Sierra / iso9660 ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-01  |  8.2 KB  |  387 lines  |  [TEXT/KAHL]

  1. /************************************************************************
  2.  *
  3.  *  Module:            main.c
  4.  *
  5.  *  Purpose:        Macintosh flow control
  6.  *
  7.  *  Description:    This module contains all the Macintosh flow control
  8.  *                    for the ISO9660 program.
  9.  *
  10.  *    Copyright © 1990 Apple Computer, Inc.  All rights reserved.
  11.  *
  12.  ************************************************************************/
  13. #include <QuickDraw.h>
  14. #include <MenuMgr.h>
  15. #include <MemoryMgr.h>
  16. #include <EventMgr.h>
  17. #include <WindowMgr.h>
  18. #include <DeskMgr.h>
  19. #include <DialogMgr.h>
  20. #include <ToolboxUtil.h>
  21.  
  22. #include <stdio.h>
  23. #include "HighSierra.h"
  24. #include "BuildISO.h"
  25. #include "DialogUtils.h"
  26.  
  27. Str255            drvName = "\p.Sony";        /* name of the device driver for isoOpen() */
  28.  
  29. enum {
  30.     AppleID = 128,
  31.     FileID,
  32.     EditID
  33. };
  34.  
  35. enum {
  36.     AppleM = 1,
  37.     FileM,
  38.     EditM
  39. };
  40.  
  41. #define    PROMPTID    258
  42.  
  43. #define    HELP        1
  44. #define CREATEFILES    2
  45. #define    QUIT        4
  46.  
  47. #define MENUCOUNT    3
  48.  
  49. MenuHandle    myMenus[MENUCOUNT];
  50. Boolean        quitProgram;
  51. EventRecord    myEvent;
  52. WindowPtr    whichWindow;
  53.  
  54. short    refNum;
  55. short    driveNumber = 0;
  56.  
  57. static void DoDisk(long);
  58. static void    InitializeVolume(void);
  59.  
  60. void SetUpMenus(void);
  61. void DoAbout(void);
  62. void DoCommand(long);
  63. void Leave(void);
  64. void HandleEvent(EventRecord *);
  65. void main(void);
  66.  
  67.  
  68. /************************************************************************
  69.  *
  70.  *  Function:        SetUpMenus
  71.  *
  72.  *  Purpose:        initialize menus
  73.  *
  74.  *  Returns:        nothing
  75.  *
  76.  *  Side Effects:    standard apple menus are set up
  77.  *
  78.  *  Description:    loop through MENUCOUNT times, inserting the menu
  79.  *                    we got using GetMenu.  Draw the menu bar.
  80.  *
  81.  ************************************************************************/
  82. void
  83. SetUpMenus()
  84. {
  85.     int    i;
  86.     
  87.     myMenus[AppleM] = GetMenu(AppleID);
  88.     AddResMenu(myMenus[AppleM], 'DRVR');    /* add desk accessories */
  89.     myMenus[FileM] = GetMenu(FileID);
  90.     myMenus[EditM] = GetMenu(EditID);        /* only used by desk access. */
  91.     
  92.     for (i = 1; i <= MENUCOUNT; i++)
  93.         InsertMenu(myMenus[i], 0);    /*install at end of Menu Bar */
  94.  
  95.     /* disable creating files until we initialize a floppy */
  96. /***TO DO***    DisableItem(myMenus[FileM], CREATEFILES); */
  97.     DrawMenuBar();
  98. }
  99.  
  100.  
  101. /************************************************************************
  102.  *
  103.  *  Function:        DoAbout
  104.  *
  105.  *  Purpose:        give ownership & copyright information
  106.  *
  107.  *  Returns:        nothing
  108.  *
  109.  *  Side Effects:    nothing
  110.  *
  111.  *  Description:    put up an alert, telling about us.
  112.  *
  113.  ************************************************************************/
  114. void
  115. DoAbout()
  116. {
  117.     Alert(DU_CenterALRT(128), 0L);
  118. }
  119.  
  120.  
  121.  
  122. /************************************************************************
  123.  *
  124.  *  Function:        DoCommand
  125.  *
  126.  *  Purpose:        Handle command or command key equivalent
  127.  *
  128.  *  Returns:        void
  129.  *
  130.  *  Side Effects:    whatever it executes may have a side effect.
  131.  *
  132.  *  Description:    Look at the menu selected and the item of that menu,
  133.  *                    and do a case statement on the item.  Execute the
  134.  *                    appropriate item.
  135.  *
  136.  ************************************************************************/
  137. void
  138. DoCommand(mResult)
  139. long mResult;
  140. {
  141.     int    theItem;
  142.     int    theMenu;
  143.     Str255 name;
  144.     GrafPtr savePort;
  145.     
  146.     theItem = LoWord(mResult);
  147.     theMenu = HiWord(mResult);
  148.     
  149.     switch (theMenu) {
  150.     case AppleID:
  151.         if (theItem == 1)     /* tell about the program */
  152.             DoAbout();
  153.         else {
  154.             GetPort(&savePort);
  155.             GetItem(myMenus[AppleM], theItem, name);
  156.             OpenDeskAcc(name);
  157.             SetPort(savePort);
  158.         }
  159.         break;
  160.     case FileID:
  161.         switch (theItem)
  162.         {
  163.             case    HELP:
  164.                 Help();
  165.                 break;
  166.             case    CREATEFILES:
  167.                 CreatePathTable(refNum);
  168.                 CreateFiles(refNum);
  169.                 /* DisableItem(myMenus[FileM], CREATEFILES); */
  170.                 break;
  171.             case    QUIT:
  172.                 quitProgram = true;
  173.                 break;
  174.         }
  175.     case EditID:
  176.         SystemEdit(theItem - 1);
  177.         break;
  178.     }
  179.     HiliteMenu(0);
  180. }
  181.  
  182.  
  183.  
  184. /************************************************************************
  185.  *
  186.  *  Function:        DoDisk
  187.  *
  188.  *  Purpose:        Handle disk inserted events
  189.  *
  190.  *  Returns:        nothing
  191.  *
  192.  *  Side Effects:    may initialize a disk
  193.  *
  194.  *  Description:    check the return code from the mount attempt (the
  195.  *                    high word of the event message coming in.)  If the
  196.  *                    mount failed, go ahead and initialize it using DIFormat.
  197.  *                    This gets the basic format information, but no file
  198.  *                    system information.
  199.  *
  200.  ************************************************************************/
  201.  static void
  202.  DoDisk(message)
  203.  long    message;
  204.  {
  205.      OSErr    result;
  206.      CursHandle    cursor;
  207.      
  208.      driveNumber = LoWord(message);
  209.      result = HiWord(message);
  210.      
  211.      /* if a mount failed (e.g. blank floppy) then we can format the floppy
  212.      ** without doing anything.  If the mount succeeded, the floppy was in
  213.      ** some format that the Mac recognized.  Unmount the volume and flush
  214.      ** it out of the mac without ejecting it, and *then* format it.
  215.      */
  216.      if (result == noErr)
  217.      {
  218.          if (AskDestroyDisk(driveNumber) == true)
  219.              result = UnmountVol(NULL, driveNumber);
  220.          else
  221.          {
  222.              UnmountVol(NULL, driveNumber);
  223.              Eject(NULL, driveNumber);
  224.              return;                /* get rid of that volume and return now! */
  225.          }
  226.      }
  227.      
  228.      cursor = GetCursor(watchCursor);
  229.      if (!cursor)
  230.          SetCursor(*cursor);
  231.      result = DIFormat(driveNumber);
  232.     InitializeVolume();
  233.     EnableItem(myMenus[FileM], CREATEFILES);
  234.     SetCursor(&arrow);
  235.          
  236.      if (result != noErr)
  237.          ErrorMsg("Can't format volume! (result = %d)", result);
  238. }
  239.  
  240.  
  241. /************************************************************************
  242.  *
  243.  *  Function:        Leave
  244.  *
  245.  *  Purpose:        allow program to recover from some bombs
  246.  *
  247.  *  Returns:        void
  248.  *
  249.  *  Side Effects:    does an exit to shell
  250.  *
  251.  *  Description:    If you are running without a debugger and get a bomb,
  252.  *                    this procedure enables the "resume" button of the 
  253.  *                    DSAlert bomb box.  It may even do the correct thing,
  254.  *                    under the circumstances.
  255.  *
  256.  ************************************************************************/
  257. void
  258. Leave()
  259. {
  260.     ExitToShell();
  261. }
  262.  
  263.  
  264. void
  265. HandleEvent(myEvent)
  266. EventRecord    *myEvent;
  267. {
  268.     DialogPtr    dPtr;
  269.     short        itemHit;
  270.     
  271.     switch (myEvent->what) {
  272.     case mouseDown:
  273.         switch (FindWindow(myEvent->where, &whichWindow)) {
  274.         case inSysWindow:
  275.             SystemClick(myEvent, whichWindow);
  276.             break;
  277.         case inMenuBar:
  278.             DoCommand(MenuSelect(myEvent->where));
  279.             break;
  280.         }    /* end MouseDown */
  281.         break;
  282.     case keyDown:
  283.     case autoKey:
  284.         if ((myEvent->modifiers & cmdKey) != 0)
  285.             DoCommand (MenuKey ((char) (myEvent->message & 0xFF)));
  286.         break;
  287.     case diskEvt:
  288.         DoDisk(myEvent->message);
  289.         break;
  290.     case updateEvt:            /* These can only happen to our dialogs */
  291.     case activateEvt:
  292.         DialogSelect(myEvent, &dPtr, &itemHit);
  293.     }    /* end myEvent.what */
  294. }
  295.  
  296. /************************************************************************
  297.  *
  298.  *  Function:        main
  299.  *
  300.  *  Purpose:        control this puppy
  301.  *
  302.  *  Returns:        none    
  303.  *
  304.  *  Side Effects:    none inherient in this routine.  Routines called
  305.  *                    from here do all the work.
  306.  *
  307.  *  Description:    Do standard Macintosh initialization.
  308.  *
  309.  ************************************************************************/
  310. void
  311. main()
  312. {
  313.     OSErr        result;
  314.     DialogPtr    dPtr;
  315.     short        itemHit;
  316.     
  317.     
  318.     InitGraf((Ptr)&thePort);
  319.     InitFonts();
  320.     FlushEvents(everyEvent, 0);
  321.     InitWindows();
  322.     InitMenus();
  323.     TEInit();
  324.     InitDialogs((ProcPtr)Leave);
  325.     InitCursor();
  326.     MoreMasters();
  327.     
  328.     SetUpMenus();
  329.  
  330.     refNum = 0;
  331.     result = isoOpen(drvName, &refNum);
  332.     if (result != noErr)
  333.     {
  334.         ErrorMsg("isoOpen failed with code %d (0x%x)", result, result);
  335.         return;
  336.     }
  337.  
  338.     quitProgram = false;
  339.     
  340.     dPtr = GetNewDialog(DU_CenterDLOG(PROMPTID), (DialogPeek)0L, (WindowPtr)-1L);
  341.     do {
  342.         SystemTask();
  343.         if (GetNextEvent(everyEvent, &myEvent)) {
  344.                 HandleEvent(&myEvent);
  345.         }    /* end if */
  346.     } while (quitProgram == false);
  347.     DisposDialog(dPtr);
  348.  
  349.     if (driveNumber != 0)
  350.     {
  351.         result = Eject(NULL, driveNumber);
  352.         if (result != noErr)
  353.             ErrorMsg("can't eject floppy! (%d)", result);
  354.     }
  355. }
  356.  
  357.  
  358. /************************************************************************
  359.  *
  360.  *  Function:        InitializeVolume
  361.  *
  362.  *  Purpose:        initialize volume
  363.  *
  364.  *  Returns:        nothing
  365.  *
  366.  *  Side Effects:    destroys volume and create PVD and VDT instead
  367.  *
  368.  *  Description:
  369.  *
  370.  ************************************************************************/
  371. static void
  372. InitializeVolume()
  373. {
  374.     OSErr    result;
  375.     
  376.     result = ZeroDisk(refNum);
  377.     if (result == noErr)
  378.     {
  379.         result = CreatePVD(refNum);
  380.         if (result == noErr)
  381.             result = CreateVDT(refNum);
  382.     }
  383.     if (result != noErr)
  384.         ErrorMsg("Couldn't initialize volume");
  385. }
  386.         
  387.